﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.usercontrols;
using Cashier_Interface;
using Cashier_Service;
using Cashier.utils;
using Cashier_Model;
using System.Collections;
using Cashier.supplier;
using Cashier.pos;

namespace Cashier.goods
{
    public partial class GoodsAddForm : FormExt, IGoodsNorm
    {
        private IReloadData iReloadData;
        private Goods goods;
        private long goodsid;
        private GoodsService goodsService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="goodsListControl"></param>
        /// <param name="goodsid"></param>
        public GoodsAddForm(MainForm mainform, IReloadData iReloadData, long goodsid)
        {
            InitializeComponent();
            textbox_stock.Text = "10000";
            this.iReloadData = iReloadData;
            this.goodsid = goodsid;
            this.goodsService = new GoodsService();
            if (goodsid > 0)
                this.button_save.Tag = "goods_edit";
            else
                this.button_save.Tag = "goods_add";

            this.treeViewCategory1.InitialTreeViewCategory();
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            this.InitialFormData();
            this.textbox_barcode.LostFocus += new EventHandler(textbox_barcode_LostFocus);
        }

        /// <summary>
        /// 初始化Form数据
        /// </summary>
        private void InitialFormData()
        {
            if (goods == null)
            {
                if (goodsid > 0)
                {
                    goods = goodsService.GetGoods(goodsid, null);
                }
            }
            if (goods != null)
            {
                FormUtil.InitialFormData(goods, this, 1);
                if (!String.IsNullOrEmpty(goods.Norm))
                    listBox_norm.Items.Add(goods.Norm);
                textbox_norm.Text = String.Empty;
                treeViewCategory1.SelectedTreeNode(goods.Categoryid.ToString());
                textbox_barcode.Enabled = false;
            }
        }

        #region 窗体控件事件

        /// <summary>
        /// 商品条码按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_barcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                String barcode = textbox_barcode.Text.Trim();
                if (!String.IsNullOrEmpty(barcode) && goodsid == 0)
                {
                    goods = goodsService.GetGoods(0, barcode);
                    if (goods != null)
                    {
                        this.goodsid = goods.Id;
                        this.InitialFormData();
                    }
                    textbox_code.Focus();
                }
            }
        }

        /// <summary>
        /// 商品条码失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_barcode_LostFocus(object sender, EventArgs e)
        {
            String barcode = textbox_barcode.Text.Trim();
            if (!String.IsNullOrEmpty(barcode) && goodsid == 0)
            {
                DataTable dt = goodsService.GetGoodsTable(0, 0, barcode, null, 0, 0M, null, 0, "id", null);
                if (dt != null && dt.Rows.Count>0)
                {
                    textbox_code.Focus();
                    GoodsNormForm goodsNormForm = new GoodsNormForm(this, dt, "norm", "id");
                    goodsNormForm.ShowDialog();
                }
                /*
                goods = goodsService.GetGoods(0, barcode);
                if (goods != null)
                {
                    this.goodsid = goods.Id;
                    this.InitialFormData();
                }
                textbox_code.Focus();
                 * */
            }
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            FormUtil.GetValidateControlsNotPassed(this, list, 1);
            if (list.Count > 0)
                return;

            String categoryid = treeViewCategory1.NodeValue;
            if (String.IsNullOrEmpty(categoryid))
            {
                MessageBox.Show("请选择类别！");
                return;
            }

            if (goods == null)
                goods = new Goods();
            goods.Isvipprice = checkBox_isvipprice.Checked;
            goods.Lasttime = DateTime.Now;
            FormUtil.GetFormData(goods, this, 1);
            goods.Categoryid = Int64.Parse(categoryid);

            String act = string.Empty;
            bool issuccess = false;
            if (goods.Id > 0)
            {
                act = "update";
                if (listBox_norm.Items.Count > 0)
                    goods.Norm = listBox_norm.Items[0].ToString();
                int flag = goodsService.UpdateGoods(goods);
                if (flag > 0)
                    issuccess = true;
            }
            else
            {
                act = "add";
                long id = 0;
                if (listBox_norm.Items.Count > 0)
                {
                    foreach (Object lopnorm in listBox_norm.Items)
                    {
                        goods.Norm = lopnorm.ToString();
                        id = goodsService.InsertGoods(goods);
                    }
                }
                else
                    id = goodsService.InsertGoods(goods);
                if (id > 0)
                {
                    FormUtil.ClearFormData(this, 1);
                    issuccess = true;
                }
            }
            if (issuccess)
            {
                DialogResult result = MessageBox.Show("保存成功，是否添加新记录！", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    goods = null;
                    FormUtil.ClearFormData(this, 1);
                    textbox_pricesale.Text = "0.00";
                    textbox_pricecost.Text = "0.00";
                    textbox_discount.Text = "1";
                    textbox_stock.Text = "10000";
                    textbox_barcode.Focus();
                }
                else
                {
                    this.Close();
                }
                iReloadData.ReloadDataGridViewData(act.Equals("add") ? -1L : 0);
            }
        }

        /// <summary>
        /// 新增商品规格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_addnorm_Click(object sender, EventArgs e)
        {
            String norm = textbox_norm.Text.Trim();
            if (!String.IsNullOrEmpty(norm))
            {
                if (goods != null && goods.Id > 0)
                    listBox_norm.Items.Clear();
                listBox_norm.Items.Add(norm);
                textbox_norm.Text = String.Empty;
            }
        }

        /// <summary>
        /// 删除商品分类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_deleteNorm_Click(object sender, EventArgs e)
        {
            if (listBox_norm.SelectedItems.Count > 0)
            {
                for (int i = 0; i < listBox_norm.SelectedItems.Count; i++ )
                {
                    listBox_norm.Items.Remove(listBox_norm.SelectedItems[i]);
                }
            }
        }

        /// <summary>
        /// ESC键退出窗体
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            bool b = base.ProcessDialogKey(keyData);
            if (keyData == Keys.Escape)
            {
                b = false;
                this.Close();
            }
            return b;
        }

        /// <summary>
        /// 备注框键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_note_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                button_save.Focus();
            }
        }

        #endregion

        #region IGoodsNorm 成员

        void IGoodsNorm.SelectGoodsNorm(object param)
        {
            if (param == null)
            {
                this.goodsid = 0;
            }
            else
            {
                DataRow row = (DataRow)param;
                this.goodsid = (Int64)row["id"];
                this.InitialFormData();
            }
        }

        #endregion
    }
}
